package com.darren.euc.action;
import java.util.Date;
import java.util.List;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.apache.log4j.Logger;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;

import com.darren.euc.entity.Bill;
import com.darren.euc.entity.BillDetail;
import com.darren.euc.entity.PageBean;
import com.darren.euc.entity.ResponseJSON;
import com.darren.euc.entity.User;
import com.darren.euc.service.BillService;
import com.darren.euc.util.PropertyHelper;
import com.darren.euc.util.StringUtil;

import net.sf.json.JSONObject;

@Controller
@RequestMapping("/bill")
public class BillAction {
	
	private static final Logger log = Logger.getLogger(BillAction.class);
	
	@Autowired
    private BillService billService;
		
	@ResponseBody
    @RequestMapping(value = "/list")
    public List<Bill> getRoleList(HttpServletRequest request,HttpServletResponse response) {
        return billService.getAll();
    }

	@SuppressWarnings("rawtypes")
	@ResponseBody
    @RequestMapping(value = "/query",method = RequestMethod.POST)
    public ResponseJSON queryBill(@RequestBody JSONObject requestJson,HttpServletRequest request,HttpServletResponse response) {	
		ResponseJSON responseJson = new ResponseJSON();
		PageBean pageBean = billService.queryForPageUseHQL(requestJson.getJSONObject("data"), PropertyHelper.getPageSize(), requestJson.getInt("currentPage"));
		responseJson.setData(pageBean);
		return responseJson;
    }
	

    @ResponseBody
    @RequestMapping(value = "/delete/detail",method = RequestMethod.POST)
    public ResponseJSON deleteBillDetail(@RequestBody JSONObject requestJson,HttpServletRequest request,HttpServletResponse response) {
    	ResponseJSON responseJson = new ResponseJSON();
    	JSONObject jsonData = new JSONObject();
    	responseJson.setData(jsonData);
    	try{
    		Integer billDetailId = Integer.parseInt(requestJson.getString("id"));
    		billService.deleteBillDetail(billDetailId);
        	responseJson.setStatus(0);
    		responseJson.setMessage("删除成功！");
    	}catch(Exception ex){
    		log.info(ex.getMessage(),ex);
        	responseJson.setStatus(-1);
    		responseJson.setMessage("删除失败！");
    	}
    	System.out.println("==============================================================");
    	System.out.println(requestJson.toString());
    	System.out.println(responseJson.toString());
    	System.out.println("==============================================================");
        return responseJson;
    }
    
	@ResponseBody
	@RequestMapping(value = "/update", method = RequestMethod.POST)
	public ResponseJSON updateBill(@RequestBody Bill bill, HttpServletRequest request,
			HttpServletResponse response) {
		ResponseJSON responseJson = new ResponseJSON();
		try {
			User user = (User) request.getAttribute("user");
			if( user != null){
				bill.setLastModifyBy(StringUtil.convertNullToEmptyString(user.getId()));
			}
			bill.setLastModifyTime(new Date());
			bill.setPaidAmount(bill.getPaidAmount());
			bill = billService.update(bill);
			responseJson.setStatus(0);
			responseJson.setMessage("保存成功！");
		} catch (Exception ex) {
			log.info(ex.getMessage(), ex);
			responseJson.setStatus(-1);
			responseJson.setMessage("保存失败！");
		}
		responseJson.setData(bill);
		System.out.println("==============================================================");
		System.out.println(responseJson.toString());
		System.out.println("==============================================================");
		return responseJson;
	}
	

	@ResponseBody
    @RequestMapping(value = "/add/detail",method = RequestMethod.POST)
    public ResponseJSON saveBillDetail(@RequestBody BillDetail billDetail,HttpServletRequest request,HttpServletResponse response) {	
		ResponseJSON responseJson = new ResponseJSON();
		try {
			User user = (User) request.getAttribute("user");
			if( user != null){
				billDetail.setCreatedBy(StringUtil.convertNullToEmptyString(user.getId()));
				billDetail.setLastModifyBy(StringUtil.convertNullToEmptyString(user.getId()));
			}
			billDetail.setCreateTime(new Date());
			billDetail.setLastModifyTime(new Date());
			billDetail = billService.saveBillDetail(billDetail);
			responseJson.setStatus(0);
			responseJson.setMessage("添加成功！");
		} catch (Exception ex) {
			log.info(ex.getMessage(), ex);
			responseJson.setStatus(-1);
			responseJson.setMessage("添加失败！");
		}
		responseJson.setData(billDetail);
		System.out.println("==============================================================");
		System.out.println(responseJson.toString());
		System.out.println("==============================================================");
		return responseJson;
    }
	
    
	@ResponseBody
	@RequestMapping(value = "/update/detail", method = RequestMethod.POST)
	public ResponseJSON updateBillDetail(@RequestBody BillDetail billDetail, HttpServletRequest request,
			HttpServletResponse response) {
		ResponseJSON responseJson = new ResponseJSON();
		try {
			User user = (User) request.getAttribute("user");
			if( user != null){
				billDetail.setLastModifyBy(StringUtil.convertNullToEmptyString(user.getId()));
			}
			billDetail.setLastModifyTime(new Date());
			billDetail = billService.updateBillDetail(billDetail);
			responseJson.setStatus(0);
			responseJson.setMessage("保存成功！");
		} catch (Exception ex) {
			log.info(ex.getMessage(), ex);
			responseJson.setStatus(-1);
			responseJson.setMessage("保存失败！");
		}
		responseJson.setData(billDetail);
		System.out.println("==============================================================");
		System.out.println(responseJson.toString());
		System.out.println("==============================================================");
		return responseJson;
	}
    
}